<template>
  <el-config-provider :locale="zhCn">
    <router-view></router-view>
  </el-config-provider>
</template>

<script lang="ts" setup>
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import { useRouter } from 'vue-router'
import Cookie from './utils/cookie'
import { TODOAUTH } from './config/constant'
import { CHECKTDAUTH } from '@/store/modules/user/types'
import { useStore } from 'vuex'

console.log('%cTodo App Start!', 'color: red;')

const router = useRouter()
const store = useStore()

router.beforeEach((to, from, next) => {
  if (!to.meta.requireAuth) {
    next()
    return
  }

  const todoAuth = Cookie.get(TODOAUTH)

  if (todoAuth) {
    store.dispatch(CHECKTDAUTH).then(
      () => { next() },
      () => { next('/login') }
    )
  } else {
    next('/login')
  }

})
</script>
